package com.jt.test;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import redis.clients.jedis.*;
import redis.clients.jedis.params.SetParams;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


public class testRedis {
    @Autowired
    private Jedis jedis;

    @Test
    void a() throws InterruptedException {

        jedis.flushDB();
        jedis.set("key1", "天天向上");
        String value = jedis.get("key1");
        if (jedis.exists("key1")) {
            jedis.set("key1", "存在key1");
        } else {
            jedis.set("key1", "不存在key1");
        }
        jedis.expire("key1", 10);
        Thread.sleep(2000);
        System.out.println(jedis.ttl("key1"));
        jedis.persist("key1");
        System.out.println("撤销成功");
    }

    /**
     * 需求: 如果数据不存在,则将数据修改
     */
    @Test
    void RedisSetNx() {
        Jedis jedis = new Jedis("192.168.126.129", 6379);
        jedis.set("key", "aaa");
        jedis.flushDB();
        jedis.setnx("key1", "测试方法");
        System.out.println(jedis.get("key1"));
    }

    /**
     * 需求: 实现超时时间的设定与赋值操作的原子性.
     * 考点: 为数据设定超时时间时,切记满足原子性要求.
     * 否则会出现key永远不能删除的现象
     */
    @Test
    void testSetEx() throws InterruptedException {
        Jedis jedis = new Jedis("192.168.126.129", 6379);
        jedis.setex("key2", 10, "bbb");
        System.out.println(jedis.get("key2"));
    }

    /**
     * 需求: 如果数据存在时,才会修改数据,并且为数据添加超时时间10秒(原子性).
     * 参数说明:
     * NX: 只有数据不存在时,才会赋值.
     * XX: 只有数据存在时,才会赋值.
     * EX: 秒
     * PX: 毫秒
     */
    @Test
    void testSet() {
        Jedis jedis = new Jedis("192.168.126.129", 6379);
//        if (jedis.exists("key3")) {
//            jedis.setex("key3", 10, "ccc");
//        }
        SetParams setParams = new SetParams();
        setParams.xx().ex(10);
        jedis.set("key3", "ccc", setParams);
    }

    /**
     * HASH测试
     */
    @Test
    void testHashSet() {
        Jedis jedis = new Jedis("192.168.126.129", 6379);
        jedis.hset("person", "id", "188");
        jedis.hset("person", "name", "tomcat");
        System.out.println(jedis.hgetAll("person"));
    }

    /**
     * LIST集合测试
     */
    @Test
    void testList() {
        Jedis jedis = new Jedis("192.168.126.129", 6379);
        jedis.lpush("list", "1,2,3,4");
        String value = jedis.rpop("list");
        System.out.println(value);
    }

    /**
     * Set集合测试
     * 1. sadd  新增元素
     * 2. SCARD  获取元素数量
     * 3. SINTER key1 key2  获取元素的交集
     * 4. SMEMBERS set      获取集合元素
     * demo自己补一下
     */

    @Test
    void testMulit() {
        Jedis jedis = new Jedis("192.168.126.129", 6379);
        Transaction transaction = jedis.multi();
        try {
            transaction.set("a", "a");
            transaction.set("b", "b");
            transaction.exec();
        } catch (Exception e) {
            e.printStackTrace();
        }
        transaction.discard();
    }


    @Test
    void testShards() {
        List<JedisShardInfo> list = new ArrayList<>();
        list.add(new JedisShardInfo("192.168.126.129", 6379));
        list.add(new JedisShardInfo("192.168.126.129", 6380));
        list.add(new JedisShardInfo("192.168.126.129", 6381));
        ShardedJedis shardedJedis = new ShardedJedis(list);
        shardedJedis.set("shards", "redis的分片的操作");
        System.out.println(shardedJedis.get("shards"));
    }


    @Test
    void testSentinel() {
        Set<String> set = new HashSet<>();
        set.add("192.168.126.129:26379");
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(1000);
        jedisPoolConfig.setMaxIdle(40);
        jedisPoolConfig.setMinIdle(10);
        JedisSentinelPool sentinelPool = new JedisSentinelPool("mymaster", set, jedisPoolConfig);
        Jedis jedis = sentinelPool.getResource();
        jedis.set("sentinel", "哨兵机制测试");
        System.out.println(jedis.get("sentinel"));
        jedis.close();
    }
    @Test
    void testCluster() {
        Set<HostAndPort> nodes= new HashSet<>();
        nodes.add(new HostAndPort("192.168.126.129", 7000));
        nodes.add(new HostAndPort("192.168.126.129", 7001));
        nodes.add(new HostAndPort("192.168.126.129", 7002));
        nodes.add(new HostAndPort("192.168.126.129", 7003));
        nodes.add(new HostAndPort("192.168.126.129", 7004));
        nodes.add(new HostAndPort("192.168.126.129", 7005));
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxTotal(1000);
        poolConfig.setMaxIdle(40);
        poolConfig.setMinIdle(20);
        JedisCluster jedisCluster = new JedisCluster(nodes);
        jedisCluster.set("jedisCluster", "redis集群搭建");
        System.out.println(jedisCluster.get("jedisCluster"));
    }
}
